# Contributor: Aiden Grossman <agrossman154@yahoo.com>
# Maintainer: Aiden Grossman <agrossman154@yahoo.com>
pkgname=freecad
pkgver=0.20.2
pkgrel=4
pkgdesc="Free and open source 3D parametric modeler"
url="https://freecadweb.org/"
license="LGPL-2.0-or-later"
arch="x86_64" # dependency OpenCascade is only x86_64
depends="
	graphviz
	hdf5
	opencascade
	py3-matplotlib
	py3-numpy
	py3-pivy
	py3-ply
	py3-pyside2
	py3-six
	py3-yaml
	python3
	"
makedepends="
	boost-dev
	cmake
	coin-dev
	doxygen
	eigen-dev
	freeimage-dev
	glu-dev
	hdf5-dev
	libmedc-dev
	libshiboken2-dev
	onetbb-dev
	opencascade-dev
	py3-pyside2-dev
	python3-dev
	shiboken2
	qt5-qtsvg-dev
	qt5-qtwebengine-dev
	qt5-qtxmlpatterns-dev
	samurai
	swig
	vtk-dev
	xerces-c-dev
	"
checkdepends="xvfb-run mesa mesa-dri-gallium font-opensans"
source="https://github.com/FreeCAD/FreeCAD/archive/$pkgver/freecad-$pkgver.tar.gz
	$pkgname-python3.11-1.patch::https://github.com/FreeCAD/FreeCAD/commit/fe02d63c8c9b1280978be841d04e68a0a55cceb9.patch
	numpy-1.20.patch
	no-execinfo.patch
	no-workaround-spnav.patch
	resourceDirectory.patch
	tests.patch
	"
builddir="$srcdir/FreeCAD-$pkgver"

build() {
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_C_FLAGS="$CFLAGS -fPIC -w" \
		-DCMAKE_CXX_FLAGS="$CXXFLAGS -fPIC -w" \
		-DCMAKE_INSTALL_PREFIX=/usr/lib/freecad \
		-DCMAKE_INSTALL_DATADIR=/usr/share/freecad \
		-DCMAKE_INSTALL_DATAROOTDIR=/usr/share \
		-DCMAKE_INSTALL_DOCDIR=/usr/share/freecad/doc \
		-DBUILD_ENABLE_CXX_STD=C++17 \
		-DBUILD_QT5=ON \
		-DFREECAD_USE_EXTERNAL_PIVY=ON \
		-DFREECAD_USE_OCC_VARIANT="Official Version" \
		-DFREECAD_USE_QT_FILEDIALOG=ON \
		-DPYTHON_EXECUTABLE=/usr/bin/python3
	cmake --build build
}

check() {
	DESTDIR=test_install cmake --install build
	mkdir -p test_install/usr/bin
	ln -s ../lib/freecad/bin/FreeCAD test_install/usr/bin/FreeCAD
	ln -s ../lib/freecad/bin/FreeCADCmd test_install/usr/bin/FreeCADCmd
	LD_LIBRARY_PATH="$PWD"/test_install/usr/lib/freecad/lib \
		xvfb-run "$PWD"/test_install/usr/bin/FreeCAD -t 0
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	# FreeCAD does not initialize correctly when binaries
	# are located under /usr/bin; thus, symlinks are necessary.
	install -d "$pkgdir"/usr/bin
	ln -s /usr/lib/freecad/bin/FreeCAD "$pkgdir"/usr/bin/FreeCAD
	ln -s /usr/lib/freecad/bin/FreeCADCmd "$pkgdir"/usr/bin/FreeCADCmd
}

sha512sums="
c3acd77dd2bb9a2a23ac354da3b6102effb89c95d675e91421d65486414dfe8cc0188a7212245e0deb63f17b9c5df76133017be09e4cd14b833be8cbec52a08d  freecad-0.20.2.tar.gz
75a237f7ed7a89a98c0e5bdb3d3f0788749602daf718089aa0814e05f93ced1e15ad5867c7c87f170b48c5984f9ace1bbc95c4f386ce72bfb8d616323b47f1e5  freecad-python3.11-1.patch
80b08b031810fce7b6d698c662f64fa4f8a904f283f46b478b1d718529164c0ee61ce190f633abf04e03212720480f3f0603b0c1e160af79d7b6bb82da3bd0e4  numpy-1.20.patch
73aaba7015dce7048eb7d2456131b5b5ba4673cc980503331987be54d99daed5f61db015ca33d7d2ef0f02bd3192da8ce122c103c3b93f9959927deb4f0b933e  no-execinfo.patch
15696bdaaf77482f1b5d3806535a8004c8cec7d598d62092d9f0394b4ca9e2ad6cedd77c4b86a83a06324d16678c1c6bbf3a390b807729717a2f513e858afd50  no-workaround-spnav.patch
8ba13b17bad66316757d180c1b9e9e72a24382627eac7c43a2264b3b5101e6e8f701775f2b805ed733f500fbcd8b0e8e422ec58a9ab3d948d613b666157d4c52  resourceDirectory.patch
5db19e0aa2ca1fd21f4c56afc9db54390a799262aaa0a741704c2c304b0068fd6ca1dcc086465e12e9c0cfe06aac750aaf9b8f5f4db324539af4dd3394803ff9  tests.patch
"
